package com.hotel.front;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.SpecialDiscountLogic;

@SuppressWarnings("serial")
public class SpecialDiscountService extends HttpServlet{	
		//http://localhost:8080/specialdiscountService?func=addSpecialDiscount&title=1&description=1&discount=1&requirement=1&isPublish=true
		//http://localhost:8080/specialdiscountService?func=editSpecialDiscount&specialdiscountId=862&title=11213331&description=1asdsad&discount=1&requirement=1&isPublish=true
		//http://localhost:8080/specialdiscountService?func=delSpecialDiscount&specialdiscountId=863
		//http://localhost:8080/specialdiscountService?func=changeSpecialDiscount&specialdiscountId=863
		public void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws IOException {
			try{
				String doClass = req.getParameter("func");
				if(doClass.equals("addSpecialDiscount")){
					AddSpecialDiscount(req,resp);
				}else if(doClass.equals("editSpecialDiscount")){
					EditSpecialDiscount(req,resp);
				}else if(doClass.equals("delSpecialDiscount")){
					DeleteSpecialDiscount(req,resp);
				}else if(doClass.equals("changeSpecialDiscount")){
					ChangeStatus(req,resp);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		private void AddSpecialDiscount(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
		{	
			HttpSession session=req.getSession(true);	
			long hotelId = (Long) session.getAttribute("hotelId");
			String title = req.getParameter("title");
			String description = req.getParameter("description");
			double discount = Double.parseDouble(req.getParameter("discount"));
			String requirement = req.getParameter("requirement");
			boolean isPublish = Boolean.parseBoolean(req.getParameter("isPublish"));
			
			Object obj = new SpecialDiscountLogic().AddSpecialDiscount(hotelId,title,description,discount,requirement,isPublish);
			Gson gson = new Gson();

			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}

		private void EditSpecialDiscount(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
		{	
			long specialdiscountId = Long.parseLong(req.getParameter("specialdiscountId"));
			String title = req.getParameter("title");
			String description = req.getParameter("description");
			double discount = Double.parseDouble(req.getParameter("discount"));
			String requirement = req.getParameter("requirement");
			boolean isPublish = Boolean.parseBoolean(req.getParameter("isPublish"));
					
			Object obj = new SpecialDiscountLogic().EditSpecialDiscount(specialdiscountId,title,description,discount,requirement,isPublish);
			Gson gson = new Gson();

			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));

		}

		private void DeleteSpecialDiscount(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
		{	
			long specialdiscountId = Long.parseLong(req.getParameter("specialdiscountId"));
			
			Object obj = new SpecialDiscountLogic().DeleteSpecialDiscount(specialdiscountId);
			Gson gson = new Gson();

			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}

		private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
		{	
			long specialdiscountId = Long.parseLong(req.getParameter("specialdiscountId"));

			Object obj = new SpecialDiscountLogic().ChangeStatus(specialdiscountId);
			Gson gson = new Gson();

			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(obj));
		}	
}